package com.cyzy.controller;

import com.cyzy.dto.Result;
import com.cyzy.pojo.Campus;
import com.cyzy.pojo.VoteRecord;
import com.cyzy.service.VoteService;
import com.cyzy.vo.CampusWorks;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description: 艺术馆的投票控制层
 * @author: yangJingShi
 * @date: 2025/6/16 下午2:22
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/artVote")
public class VoteController {

    @Autowired
    private VoteService voteService;

    /**
     * @description: 查询活动的投票(包括投票情况,参与人数,投票总数)
     * @author: yangJingShi
     * @date: 2025/6/16 下午2:23
     * @param: []
     * @return: com.cyzy.dto.Result
     **/
    @GetMapping("/getAllVote")
    @Transactional(rollbackFor = Exception.class)
    public Result getAllVote(@Param("exhId") Long exhId){
        try {
            System.out.println("当前的活动ID为:"+exhId);
            //查询该活动投票作品列表
            Map<String, Object> map = voteService.selectAllVote(exhId);
            System.out.println(map);


            return Result.ok().message("查询对应校区最近的一场投票").data("map",map);
        }catch (Exception e){
            throw e;
        }
    }

    /**
     * @description: 判断用户的某一个作品投票情况(true:投过票，false：没投过)
     * @author: yangJingShi
     * @date: 2025/6/18 上午10:13
     * @param: []
     * @return: com.cyzy.dto.Result
     **/
    @GetMapping("/judegeUserVote")
    public Result judegeUserVote(@Param("exhId") Long exhId,
                                 @Param("workId") Long workId,
                                 @Param("campusId") Long campusId,
                                 @Param("teacherId") Long teacherId,
                                 @Param("parentId") Long parentId){
        VoteRecord record = new VoteRecord();
        record.setExhId(exhId);
        record.setCampusId(campusId);
        record.setParentId(parentId);
        record.setTeacherId(teacherId);
        record.setWorkId(workId);
        System.out.println(record);
        Boolean judgeUserVote = voteService.judgeUserVote(record);
        String Str;
        if (judgeUserVote){
            Str="用户已经投票过该作品";
        }else {
            Str="用户未投票";
        }
        return Result.ok().data("judege",judgeUserVote).message(Str);
    }

    /**
     * @description: 老师或者用户发起投票
     * @author: yangJingShi
     * @date: 2025/6/17 下午5:00
     * @param: [exhId, workId, parentId, teacherId]
     * @return: com.cyzy.dto.Result
     **/
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/userVoted")
    public Result userVoted(@RequestBody VoteRecord voteUser) {
        try {
            System.out.println(voteUser);
            if (voteUser.getParentId()==0&&voteUser.getTeacherId()!=0){
                System.out.println("老师投票");
            }else if (voteUser.getParentId()!=0&&voteUser.getTeacherId()==0){
                System.out.println("家长投票");
            }else {
                System.out.println("投票出错");
                return Result.ok().message("投票出错");
            }

            //判断该账号对于该画展的作品是否投票过(true:投过票，false：未投过)
            Boolean judgeUserVote = voteService.judgeUserVote(voteUser);
            //有则返回该账号已经投过票了
            if (judgeUserVote){
                System.out.println("该用户投过票");
                return Result.ok().message("该用户投过票");
            }else{
                System.out.println("该用户还没投票");
                //新增一条投票记录
                Boolean b = voteService.insertNewVoteRecord(voteUser);
                if (b){
                    return Result.ok().message("投票成功");
                }else{
                    return Result.ok().message("投票失败");
                }
            }
        }catch (Exception e){
            throw e;
        }
    }

    /*
     * @description: 查询校区信息
     * @author: yangJingShi
     * @date: 2025/6/22 下午12:21
     * @param: [campusId]
     * @return: com.cyzy.dto.Result
     **/
    @GetMapping("/queryCampusInfo")
    public Result queryCampusInfo(@RequestParam("campusId") Long campusId){

        Campus campus = voteService.queryCampusInfo(campusId);
        return Result.ok().message("查询校区信息").data("campus",campus);
    }

    /*
     * @description: 查询校区全部的投票作品
     * @author: yangJingShi
     * @date: 2025/6/22 下午12:33
     * @param: [campusId]
     * @return: com.cyzy.dto.Result
     **/
    @GetMapping("/queryCampusVoteInfo")
    public Result queryCampusVoteInfo(@RequestParam("campusId") Long campusId){
        return Result.ok().message("查询校区全部的投票信息").data("voteList",voteService.queryCampusVoteInfo(campusId));
    }
}
